package UserService

import (
	"QingDai-Gin/api/models"
	"QingDai-Gin/config"
	"gorm.io/gorm"
)

func GetAllUser() ([]models.User, *gorm.DB) {
	var users []models.User
	Mysql := config.Mysql()
	tx := Mysql.Table("user__user").Find(&users)
	return users, tx
}

func GetUserById(id int) (models.User, *gorm.DB) {
	var user models.User
	Mysql := config.Mysql()
	tx := Mysql.Table("user__user").First(&user, id)
	return user, tx
}

func GetUsersByP(p map[string]any) ([]models.User, *gorm.DB) {
	var users []models.User
	Mysql := config.Mysql()
	tx := Mysql.Table("user__user")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.Find(&users)
	return users, tx
}
func GetUserByP(p map[string]any) (models.User, *gorm.DB) {
	var user models.User
	Mysql := config.Mysql()
	tx := Mysql.Table("user__user")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.First(&user)
	return user, tx
}

func CreateUser(user models.User) {
	Mysql := config.Mysql()
	Mysql.Table("user__user").Create(user)
}

func UpdateUser(user models.User) {
	Mysql := config.Mysql()
	Mysql.Table("user__user").Save(user)
}

func DeleteUser(user models.User) {
	Mysql := config.Mysql()
	Mysql.Table("user__user").Delete(user)
}
